<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class XmlData extends CI_Controller{

    function cronexectest($name="xxx"){
        if(!$this->input->is_cli_request()){
            echo 'Not run in cli!!!';
            return;
        }
        echo "Hello ,".$name.PHP_EOL;
    }
    
    /**
     * 定时任务处理导入的ftp文件解析并将记录插入数据库
     */
	function cronrecordimport(){
	    if(!$this->input->is_cli_request()){
	        echo 'Not run in cli!!!';
	        return;
	    }
	    
// 	    date_default_timezone_set('Etc/GMT-8');
	    date_default_timezone_set('Etc/GMT+4');
	    $this->config->load('ftpxmldata');
	    $this->load->helper('xml');
	    $this->load->helper('directory');
	    $file_root_dir = $this->config->item('ftpXmlFileRootDir');
	    $data_type_arr = $this->config->item('ftpXmlDataType');
	    $ftpXmlP1UserReward = $this->config->item('ftpXmlP1UserReward');
	    $ftpXmlP2UserReward = $this->config->item('ftpXmlP2UserReward');
	    //处理开始时间命名的文件夹的
	    $dir_date_begin = date("Ymd",strtotime("-".$this->config->item('ftpXmlDateBegin')." day"));
	    $file_date_begin = date("YmdHi",strtotime("-".$this->config->item('ftpXmlDateBegin')." day"));
	    //处理结束时间命名的文件夹的
	    $dir_date_end = date("Ymd",strtotime("-".$this->config->item('ftpXmlHandleRecordHoursAgo')." hours"));
	    $file_date_end = date("YmdHi",strtotime("-".$this->config->item('ftpXmlHandleRecordHoursAgo')." hours"));
	    $gamePlatDirArr = directory_map($file_root_dir,1);
	    //开始日期到结束日期范围内的日期列表
	    $dirDateArr = $this->_getDateFromRange($dir_date_begin, $dir_date_end);
	    //遍历平台文件夹下指定日期目录的目录
	    foreach ($gamePlatDirArr as $gamePlatDir){
	        foreach ($dirDateArr as $dirDate){
	            $gameFileDir = $file_root_dir.$gamePlatDir.DIRECTORY_SEPARATOR.$dirDate;
	            if(is_dir($gameFileDir)){
	                echo 'dir "'.$gameFileDir.'" exsists</br>';
	                //遍历当前日期目录下的所有文件
	                $ftpFilesArr = directory_map($gameFileDir);
	                foreach ($ftpFilesArr as $file){
	                    $file_name = pathinfo($file)['filename'];
	                    $file_date = $file_name;
	                    if($file_date < $file_date_begin || $file_date > $file_date_end) continue; //指定天数以外的不处理
	                    $file_name = $gameFileDir.DIRECTORY_SEPARATOR.$file_name;
	                    if(IOSS_GameRecord::getRecordOpLog($file_name)) continue;  //操作日志标记已经处理文件了，不需要处理
	                    $opId = IOSS_GameRecord::replaceIntoOpLog($file_name);
	                    if(!$opId) continue;   //操作日志没有插入成功，不处理
	                    $ftpXmlFile = $gameFileDir.DIRECTORY_SEPARATOR.$file;
	                    $xmlFileStr = '<root>'.file_get_contents($ftpXmlFile).'</root>';
	                    $xmlObj = simplexml_load_string($xmlFileStr);
	                    //遍历当前文件中每条记录
	                    foreach ($xmlObj as $row){
	                        $dataType = (string)$row['dataType'];
	                        if(!in_array($dataType, $data_type_arr)) continue;    //非下注记录不处理
	                        if((int)$row['flag'] == 0) continue;    //flag=0未结算，不处理
	                        //入库操作
	                        $obj = new stdClass();
	                        $obj->account = substr((string)$row['playerName'],2);  //我们平台真实账号是游戏传来的去掉前缀aj后的手机号
	                        $obj->dataType = $dataType;
	                        $obj->billNo = (string)$row['billNo'];
	                        $obj->mainBillNo = (string)$row['mainbillno'];
	                        $obj->betTime = strtotime((string)$row['betTime']);
	                        $obj->netAmount = (int)$row['netAmount'];
	                        $obj->opId = $opId;
	                        $obj->ftpXmlP1UserReward = $ftpXmlP1UserReward;
	                        $obj->ftpXmlP2UserReward = $ftpXmlP2UserReward;
	                        IOSS_GameRecord::insertIgnoreRecord($obj);
	                    }
	                    //插入完成后，修改操作记录日志状态为完成
	                    IOSS_GameRecord::finishOpLog($file_name);
	                }
	            }else{
	                echo 'dir "'.$gameFileDir.'" not exsists</br>';
	            }
	            
	        }
	    }
	    echo 'cronrecordimport action done!';
	}
	
	/**
	 * 处理记录表中status=0未处理的记录，将提成给上级上上级后修改status=1已完成
	 */
	function cronhandlerecord(){
	     if(!$this->input->is_cli_request()){
	     echo 'Not run in cli!!!';
	     return;
	     }
	    $this->config->load('ftpxmldata');
	    $ftpXmlAffiliateUpdateHoursAgo = $this->config->item('ftpXmlAffiliateUpdateHoursAgo');
	    IOSS_GameRecord::handleRecords($ftpXmlAffiliateUpdateHoursAgo);
	    echo 'cronhandlerecord action done!';
	}
	
	/**
	* 获取指定日期段内每一天的日期
	* @param  Date  $startdate 开始日期
	* @param  Date  $enddate   结束日期
	* @return Array
	*/
	private function _getDateFromRange($startdate, $enddate){
	    $stimestamp = strtotime($startdate);
	    $etimestamp = strtotime($enddate);
	    // 计算日期段内有多少天
	    $days = ($etimestamp-$stimestamp)/86400+1;
	    // 保存每天日期
	    $date = array();
	    for($i=0; $i<$days; $i++){
	        $date[] = date('Ymd', $stimestamp+(86400*$i));
	    }
	    return $date;
	}
}
